<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceSn">
        <el-input
          v-model="queryParams.deviceSn"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型名称, 如猫眼、窗帘、分集水器" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型名称, 如猫眼、窗帘、分集水器" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="家庭id/社区园区id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入家庭id/社区园区id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomNum">
        <el-input
          v-model="queryParams.roomNum"
          placeholder="请输入房间号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分组id" prop="groupId">
        <el-input
          v-model="queryParams.groupId"
          placeholder="请输入分组id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备源 0 家庭设备 1 非家庭设备" prop="deviceSource">
        <el-input
          v-model="queryParams.deviceSource"
          placeholder="请输入设备源 0 家庭设备 1 非家庭设备"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="1 网关 2 网关子设备 3 wifi" prop="deviceCategory">
        <el-input
          v-model="queryParams.deviceCategory"
          placeholder="请输入1 网关 2 网关子设备 3 wifi"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入设备品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specModel">
        <el-input
          v-model="queryParams.specModel"
          placeholder="请输入规格型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂商" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入厂商"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂商编号" prop="manufacturerNo">
        <el-input
          v-model="queryParams.manufacturerNo"
          placeholder="请输入厂商编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产时间" prop="productTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.productTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择生产时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="保修期开始时间" prop="maintainStarttime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.maintainStarttime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择保修期开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="保修期结束时间" prop="maintainEndtime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.maintainEndtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择保修期结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备描述" prop="deviceDescription">
        <el-input
          v-model="queryParams.deviceDescription"
          placeholder="请输入设备描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IMEI" prop="imei">
        <el-input
          v-model="queryParams.imei"
          placeholder="请输入IMEI"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="1在线设备/0消防设备" prop="deviceOnline">
        <el-input
          v-model="queryParams.deviceOnline"
          placeholder="请输入1在线设备/0消防设备"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="在线数据" prop="ontimeData">
        <el-input
          v-model="queryParams.ontimeData"
          placeholder="请输入在线数据"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="deviceStatus">
        <el-select v-model="queryParams.deviceStatus" placeholder="请选择设备状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="区域名称" prop="regionName">
        <el-input
          v-model="queryParams.regionName"
          placeholder="请输入区域名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BIM编号" prop="bimNo">
        <el-input
          v-model="queryParams.bimNo"
          placeholder="请输入BIM编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入经度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入纬度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购时间" prop="purchaseTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.purchaseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择采购时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="采购价格" prop="purchasePrice">
        <el-input
          v-model="queryParams.purchasePrice"
          placeholder="请输入采购价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维保单位" prop="maintainCompany">
        <el-input
          v-model="queryParams.maintainCompany"
          placeholder="请输入维保单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域id" prop="regionId">
        <el-input
          v-model="queryParams.regionId"
          placeholder="请输入区域id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建筑物id" prop="buildingId">
        <el-input
          v-model="queryParams.buildingId"
          placeholder="请输入建筑物id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备数据子系统  1* 火 2* 水  3* 电  4*通道" prop="subSystemType">
        <el-select v-model="queryParams.subSystemType" placeholder="请选择设备数据子系统  1* 火 2* 水  3* 电  4*通道" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['buz:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['buz:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['buz:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['buz:device:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备数据子系统  1* 火 2* 水  3* 电  4*通道" align="center" prop="deviceId" />
      <el-table-column label="设备编号" align="center" prop="deviceSn" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备类型名称, 如猫眼、窗帘、分集水器" align="center" prop="deviceType" />
      <el-table-column label="家庭id/社区园区id" align="center" prop="deptId" />
      <el-table-column label="房间号" align="center" prop="roomNum" />
      <el-table-column label="分组id" align="center" prop="groupId" />
      <el-table-column label="设备源 0 家庭设备 1 非家庭设备" align="center" prop="deviceSource" />
      <el-table-column label="1 网关 2 网关子设备 3 wifi" align="center" prop="deviceCategory" />
      <el-table-column label="设备品牌" align="center" prop="brand" />
      <el-table-column label="规格型号" align="center" prop="specModel" />
      <el-table-column label="厂商" align="center" prop="manufacturer" />
      <el-table-column label="厂商编号" align="center" prop="manufacturerNo" />
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="生产时间" align="center" prop="productTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保修期开始时间" align="center" prop="maintainStarttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintainStarttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保修期结束时间" align="center" prop="maintainEndtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintainEndtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备描述" align="center" prop="deviceDescription" />
      <el-table-column label="IMEI" align="center" prop="imei" />
      <el-table-column label="1在线设备/0消防设备" align="center" prop="deviceOnline" />
      <el-table-column label="在线数据" align="center" prop="ontimeData" />
      <el-table-column label="设备状态" align="center" prop="deviceStatus" />
      <el-table-column label="区域名称" align="center" prop="regionName" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="维保单位" align="center" prop="maintainCompany" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['buz:device:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['buz:device:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="deviceSn">
          <el-input v-model="form.deviceSn" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备类型名称, 如猫眼、窗帘、分集水器" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择设备类型名称, 如猫眼、窗帘、分集水器">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="家庭id/社区园区id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入家庭id/社区园区id" />
        </el-form-item>
        <el-form-item label="房间号" prop="roomNum">
          <el-input v-model="form.roomNum" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="分组id" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入分组id" />
        </el-form-item>
        <el-form-item label="设备源 0 家庭设备 1 非家庭设备" prop="deviceSource">
          <el-input v-model="form.deviceSource" placeholder="请输入设备源 0 家庭设备 1 非家庭设备" />
        </el-form-item>
        <el-form-item label="1 网关 2 网关子设备 3 wifi" prop="deviceCategory">
          <el-input v-model="form.deviceCategory" placeholder="请输入1 网关 2 网关子设备 3 wifi" />
        </el-form-item>
        <el-form-item label="设备品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入设备品牌" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specModel">
          <el-input v-model="form.specModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="厂商" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入厂商" />
        </el-form-item>
        <el-form-item label="厂商编号" prop="manufacturerNo">
          <el-input v-model="form.manufacturerNo" placeholder="请输入厂商编号" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="生产时间" prop="productTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.productTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生产时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保修期开始时间" prop="maintainStarttime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.maintainStarttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择保修期开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保修期结束时间" prop="maintainEndtime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.maintainEndtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择保修期结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备描述" prop="deviceDescription">
          <el-input v-model="form.deviceDescription" placeholder="请输入设备描述" />
        </el-form-item>
        <el-form-item label="IMEI" prop="imei">
          <el-input v-model="form.imei" placeholder="请输入IMEI" />
        </el-form-item>
        <el-form-item label="1在线设备/0消防设备" prop="deviceOnline">
          <el-input v-model="form.deviceOnline" placeholder="请输入1在线设备/0消防设备" />
        </el-form-item>
        <el-form-item label="在线数据" prop="ontimeData">
          <el-input v-model="form.ontimeData" placeholder="请输入在线数据" />
        </el-form-item>
        <el-form-item label="设备状态">
          <el-radio-group v-model="form.deviceStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="区域名称" prop="regionName">
          <el-input v-model="form.regionName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="BIM编号" prop="bimNo">
          <el-input v-model="form.bimNo" placeholder="请输入BIM编号" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="采购时间" prop="purchaseTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.purchaseTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择采购时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="采购价格" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入采购价格" />
        </el-form-item>
        <el-form-item label="维保单位" prop="maintainCompany">
          <el-input v-model="form.maintainCompany" placeholder="请输入维保单位" />
        </el-form-item>
        <el-form-item label="区域id" prop="regionId">
          <el-input v-model="form.regionId" placeholder="请输入区域id" />
        </el-form-item>
        <el-form-item label="建筑物id" prop="buildingId">
          <el-input v-model="form.buildingId" placeholder="请输入建筑物id" />
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="设备数据子系统  1* 火 2* 水  3* 电  4*通道" prop="subSystemType">
          <el-select v-model="form.subSystemType" placeholder="请选择设备数据子系统  1* 火 2* 水  3* 电  4*通道">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/buz/device";

export default {
  name: "Device",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceSn: null,
        deviceName: null,
        deviceType: null,
        deptId: null,
        roomNum: null,
        groupId: null,
        deviceSource: null,
        deviceCategory: null,
        brand: null,
        specModel: null,
        manufacturer: null,
        manufacturerNo: null,
        supplier: null,
        productTime: null,
        maintainStarttime: null,
        maintainEndtime: null,
        deviceDescription: null,
        imei: null,
        deviceOnline: null,
        ontimeData: null,
        deviceStatus: null,
        regionName: null,
        bimNo: null,
        longitude: null,
        latitude: null,
        purchaseTime: null,
        purchasePrice: null,
        maintainCompany: null,
        regionId: null,
        buildingId: null,
        floor: null,
        subSystemType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceSn: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: null,
        deviceSn: null,
        deviceName: null,
        deviceType: null,
        deptId: null,
        roomNum: null,
        groupId: null,
        deviceSource: null,
        deviceCategory: null,
        brand: null,
        specModel: null,
        manufacturer: null,
        manufacturerNo: null,
        supplier: null,
        productTime: null,
        maintainStarttime: null,
        maintainEndtime: null,
        createTime: null,
        deviceDescription: null,
        createBy: null,
        imei: null,
        deviceOnline: null,
        ontimeData: null,
        deviceStatus: "0",
        regionName: null,
        bimNo: null,
        longitude: null,
        latitude: null,
        purchaseTime: null,
        purchasePrice: null,
        maintainCompany: null,
        regionId: null,
        buildingId: null,
        floor: null,
        subSystemType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceId = row.deviceId || this.ids
      getDevice(deviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceId != null) {
            updateDevice(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDevice(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$confirm('是否确认删除设备编号为"' + deviceIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDevice(deviceIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buz/device/export', {
        ...this.queryParams
      }, `buz_device.xlsx`)
    }
  }
};
</script>